# 向量搜索
import random
from pymilvus import Collection, connections


# 连接到Milvus数据库 注意命名关键字时db_name！
connections.connect("default", host="localhost", port="19530", db_name="test")
# 获取名为example的集合对象
collection = Collection("example")

# 定义搜索参数配置
search_params = {"metric_type": "COSINE", "params": {"nprobe": 10}}

# 生成一个查询向量
query_vector = [random.random() for _ in range(128)]

# 执行向量搜索操作
results = collection.search(data=[query_vector], anns_field="embedding", param=search_params, limit=5)

for hits in results:
    for hit in hits:
        # hit 结构：{'id': 459215927134406700, 'distance': 20.738426208496094, 'entity': {}}
        print(hit)
